/**********************************************************************
* Filename: Realizacao.java
* Created: 2013/05/30
* @author Fernando Brito e Abreu
**********************************************************************/
package org.quasar.airNova.businessLayer;

import org.quasar.airNova.persistenceLayer.Database;

import java.util.Set;
import java.util.HashSet;

public class Realizacao
{
	
	/***********************************************************
	* @return all instances of class Realizacao
	***********************************************************/
	public static Set<Realizacao> allInstances()
	{
		return Database.allInstances(Realizacao.class);
	}
	
	private Voo realiza_se;
	private CalendarDate ocorre_em;
	private Aviao aviao;
	private boolean cancelada;
	
	/**********************************************************************
	* Associative constructor
	* @param realiza_se the realiza_se to initialize
	* @param ocorre_em the ocorre_em to initialize
	**********************************************************************/
	public Realizacao(Voo realiza_se, CalendarDate ocorre_em)
	{
		this.realiza_se = realiza_se;
		this.ocorre_em = ocorre_em;
	}
	
	/**********************************************************************
	* Parameterized constructor
	* @param realiza_se the realiza_se to initialize
	* @param ocorre_em the ocorre_em to initialize
	* @param aviao the aviao to initialize
	* @param cancelada the cancelada to initialize
	**********************************************************************/
	public Realizacao(Voo realiza_se, CalendarDate ocorre_em, Aviao aviao, boolean cancelada)
	{
		this.realiza_se = realiza_se;
		this.ocorre_em = ocorre_em;
		this.aviao = aviao;
		this.cancelada = cancelada;
	}
	
	/**********************************************************************
	* ASSOCIATIVE2MEMBER getter for Realizacao[*] <-> Voo[1]
	* @return the realiza_se of the realizacao
	**********************************************************************/
	public Voo realiza_se()
	{
		return realiza_se;
	}
	
	/**********************************************************************
	* ASSOCIATIVE2MEMBER setter for Realizacao[*] <-> Voo[1]
	* @param realiza_se the realiza_se to set
	**********************************************************************/
	public void setRealiza_se(Voo realiza_se)
	{
		this.realiza_se = realiza_se;
	}
	
	/**********************************************************************
	* ASSOCIATIVE2MEMBER getter for Realizacao[*] <-> CalendarDate[1]
	* @return the ocorre_em of the realizacao
	**********************************************************************/
	public CalendarDate ocorre_em()
	{
		return ocorre_em;
	}
	
	/**********************************************************************
	* ASSOCIATIVE2MEMBER setter for Realizacao[*] <-> CalendarDate[1]
	* @param ocorre_em the ocorre_em to set
	**********************************************************************/
	public void setOcorre_em(CalendarDate ocorre_em)
	{
		this.ocorre_em = ocorre_em;
	}
	
	/**********************************************************************
	* ONE2MANY getter for Realizacao[*] <-> Aviao[1]
	* @return the aviao of the realizacao
	**********************************************************************/
	public Aviao aviao()
	{
		return aviao;
	}
	
	/**********************************************************************
	* ONE2MANY setter for Realizacao[*] <-> Aviao[1]
	* @param aviao the aviao to set
	**********************************************************************/
	public void setAviao(Aviao aviao)
	{
		this.aviao = aviao;
	}
	
	/**********************************************************************
	* Standard attribute getter
	* @return the cancelada of the realizacao
	**********************************************************************/
	public boolean cancelada()
	{
		return cancelada;
	}
	
	/**********************************************************************
	* Standard attribute setter
	* @param cancelada the cancelada to set
	**********************************************************************/
	public void setCancelada(boolean cancelada)
	{
		this.cancelada = cancelada;
	}
	
	/**********************************************************************
	* ONE2MANY getter for Realizacao[1] <-> Bilhete[*]
	* @return the bilhetes of the realizacao
	**********************************************************************/
	public Set<Bilhete> bilhetes()
	{
		Set<Bilhete> result = new HashSet<Bilhete>();
		for (Bilhete x : Bilhete.allInstances())
			if (x.realizacao()  ==  this)
				result.add(x);
		return result;
	}
	
	/**********************************************************************
	* ONE2MANY multiple setter for Realizacao[1] <-> Bilhete[*]
	* @param bilhetes the bilhetes to set
	**********************************************************************/
	public void setBilhetes(Set<Bilhete> bilhetes)
	{
		for (Bilhete x : bilhetes)
			x.setRealizacao(this);
	}
	
	/**********************************************************************
	* ONE2MANY single setter for Realizacao[1] <-> Bilhete[*]
	* @param bilhete the bilhete to add
	**********************************************************************/
	public void addBilhetes(Bilhete bilhete)
	{
		bilhete.setRealizacao(this);
	}
	
	/**********************************************************************
	* Object serializer
	**********************************************************************/
	public String toString()
	{
		return "Realizacao [realiza_se=" + realiza_se + ", ocorre_em=" + ocorre_em + ", aviao=" + aviao + ", cancelada=" + cancelada + "]";
	}
	
	//	-------------------------------------------------------------------------------
	//	INVARIANTS (TODO)
	/*
	inv soSeRealizaSeONumeroDeBilhetesForSuperiorAUmEInferiorAoNumeroDeBilhetesDaClasse
		if self.bilhetes->notEmpty then self.aviao.classe->collectNested($e : Classe | $e.capacidade)->flatten->forAll(c1 : Capacidade | (c1.Lotacao() >= c1.classe.bilhetes->size)) else (self.cancelada = true) endif
	
	inv soPodeTerBilhetesSeAReservaNaoForFalse
		if (self.cancelada = false) then (self.bilhetes->isEmpty or self.bilhetes->notEmpty) else self.bilhetes->isEmpty endif
	
	*/
}
